package com.factory.iamp.dao;

import com.factory.iamp.pojo.Upload;
import com.factory.iamp.pojo.UploadList;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UploadMapper {
    @Select("select count(fid) from upload")
    int Count();

    @Select("select count(fid) from upload where uid = #{uid}")
    int MyCount(@Param("uid") String uid);

    @Select("select * from upload where fid = #{fid} limit 1")
    Upload findById(@Param("fid") int fid);

    @Select("select * from upload where fid = #{fid} and uid = #{uid} limit 1")
    Upload findMyById(@Param("fid") int fid, @Param("uid") String uid);

    @Select("select * from upload where tid = #{tid}")
    @Results({
            @Result(property = "user", column = "uid", one = @One(select = "com.factory.iamp.dao.UserMapper.findById"))
    })
    List<UploadList> getTableFile(@Param("tid") int tid);

    @Select("select * from upload where oid = #{oid}")
    @Results({
            @Result(property = "user", column = "uid", one = @One(select = "com.factory.iamp.dao.UserMapper.findById"))
    })
    List<UploadList> getContentFile(@Param("oid") int oid);

    @Select("select * from upload limit #{offset}, #{limit}")
    @Results({
            @Result(property = "user", column = "uid", one = @One(select = "com.factory.iamp.dao.UserMapper.findById"))
    })
    List<UploadList> ListPaged(@Param("offset") int offset, @Param("limit") int limit);

    @Select("select * from upload where uid = #{uid} limit #{offset}, #{limit}")
    @Results({
            @Result(property = "user", column = "uid", one = @One(select = "com.factory.iamp.dao.UserMapper.findById"))
    })
    List<UploadList> MyListPaged(@Param("offset") int offset, @Param("limit") int limit, @Param("uid") String uid);

    @Insert("insert into upload(uid, tid, realname, path) values (#{uid}, #{tid}, #{realname}, #{path})")
    @Options(useGeneratedKeys = true, keyProperty = "fid", keyColumn = "fid")
    void addFileSingle(Upload upload);

    @Delete("delete from upload where fid = #{fid}")
    void deleteById(@Param("fid") int fid);
}
